package com.cennavi.minemap.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.GroupSourceLayerBean;
import com.cennavi.minemap.bean.SourceBean;
import com.cennavi.minemap.bean.SourceGroupBean;
import com.cennavi.minemap.bean.UserSourceBean;
import com.cennavi.minemap.dao.SourceDao;
import com.cennavi.minemap.util.FileUtil;

@Service
@ComponentScan(basePackages = { "com.cennavi.minemap.dao" })
@PropertySource(value = { "classpath:application.properties" })
public class SourceService {
	@Autowired
	private SourceDao sourceDao;

	@Value("${spring.statistic.path}")
	private String statisticPath;

	public int update(SourceBean sourceBean) {

		SourceBean oldSource = sourceDao.queryById(sourceBean.getId());

		if (oldSource.getUploadId() > 0) {

			JSONArray layerDesc = JSONArray.parseArray(oldSource.getLayerDesc());

			JSONArray newlayerDesc = new JSONArray();

			for (int i = 0; i < layerDesc.size(); i++) {
				JSONObject json = layerDesc.getJSONObject(i);
				json.put("name", sourceBean.getName());
				newlayerDesc.add(json);
			}
			sourceBean.setLayerDesc(newlayerDesc.toJSONString());
		}
		return sourceDao.update(sourceBean);
	}

	public List<SourceBean> queryById(int id) {
		return Arrays.asList(sourceDao.queryById(id));
	}

	public List<JSONObject> queryStatisticById(int id) {
		SourceBean sourceBean = sourceDao.queryById(id);
		if (sourceBean == null) {
			return null;
		}

		List<JSONObject> retList = new ArrayList<>();

		String code = sourceBean.getCode();
		List<String> files = FileUtil.getFileList(statisticPath, code.toLowerCase() + "_", ".json");
		for (String sFile : files) {
			String fileData = "";
			try {
				File file = new File(sFile);
				if (!file.exists()) {
					return null;
				}
				BufferedReader bufferReader = new BufferedReader(new FileReader(file));
				String strLine = "";

				while ((strLine = bufferReader.readLine()) != null) {
					fileData += strLine;
				}

				bufferReader.close();
				JSONObject obj = JSONObject.parseObject(fileData);

				if (obj != null) {
					retList.add(obj);
				}
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
		return retList;
	}

	public List<SourceBean> queryByUserGroupId(int userId, int groupId) {
		return sourceDao.queryByUserGroupId(userId, groupId, false);
	}

	public List<SourceBean> querySelfByUserGroupId(final int userId) {
		return sourceDao.querySelfByUserGroupId(userId);
	}

	public String querySourceLayerDescById(int id) {
		return sourceDao.querySourceLayerDescById(id);
	}

	public int sourceBuy(UserSourceBean userSourceBean) {
		return sourceDao.sourceBuy(userSourceBean);
	}

	public List<SourceGroupBean> queryGroup() {
		return sourceDao.queryGroup(false);
	}

	public List<SourceGroupBean> queryGroupByUserId(int userId) {
		return sourceDao.queryGroupByUserId(userId);
	}

	public String queryTileJsonById(int id) {
		return sourceDao.queryTileJsonById(id);
	}

	public List<GroupSourceLayerBean> queryGroupSourceLayerBy(int userId) {

		List<GroupSourceLayerBean> list = new ArrayList<>();

		List<SourceGroupBean> sourceGroupBeans = sourceDao.queryGroup(true);

		for (SourceGroupBean sourceGroupBean : sourceGroupBeans) {
			GroupSourceLayerBean groupSourceLayerBean = new GroupSourceLayerBean(sourceGroupBean);
			int groupId = sourceGroupBean.getId();
			List<SourceBean> sourceBeans = sourceDao.queryByUserGroupId(userId, groupId, true);
			groupSourceLayerBean.setSources(sourceBeans);
			list.add(groupSourceLayerBean);
		}
		return list;
	}
}
